import type { BasicQueryParams, BasicResult, PaginationResult } from '../core';

import { requestClient } from '../request';

export namespace UserApi {
  const URL = '/auth/user';
  /** 登录接口返回值 */
  export interface UserResult extends BasicResult {
    username: string;
    nickname: string;
    email: string;
    phone: string;
    // groups: string[];
  }
  // 修改密码参数
  export interface ChangePasswordParams {
    old_password: string;
    new_password: string;
  }
  /**
   * 查询用户
   */
  export async function listUser(params: BasicQueryParams) {
    return requestClient.get<PaginationResult<UserResult>>(URL, {
      params,
      withCredentials: true,
    });
  }

  /**
   * 获取用户
   */
  export async function retrieveUser(id: string) {
    return requestClient.get<UserResult>(`${URL}/${id}`, {
      withCredentials: true,
    });
  }

  /**
   * 创建用户
   */
  export async function createUser(data: Partial<UserResult>) {
    return requestClient.post<UserResult>(`${URL}`, data, {
      withCredentials: true,
    });
  }
  /**
   * 更新用户
   */
  export async function updateUser(id: string, data: Partial<UserResult>) {
    return requestClient.patch<UserResult>(`${URL}/${id}`, data, {
      withCredentials: true,
    });
  }

  /**
   * 修改密码
   */
  export async function changePassword(id: string) {
    return requestClient.post<UserResult>(`${URL}/${id}/change_password`, {
      withCredentials: true,
    });
  }
  /**
   * 重置用户
   */
  export async function resetPassword(id: string) {
    return requestClient.post<string>(`${URL}/${id}/reset_password`, {
      withCredentials: true,
    });
  }

  /**
   * 更新用户组
   */
  export async function updateUserGroups(id: string, data: { groups: string[] }) {
    return requestClient.post<UserResult>(`${URL}/${id}/group`, data, {
      withCredentials: true,
    });
  }
}
